const request = require('./request')
module.exports = {

  // 获取歌手专辑
  album: async (query) => {
    const {singermid, pageNo = 1, pageSize = 20, raw} = query;

    if (!singermid) {
      return {
        result: 500,
        errMsg: 'singermid 不能为空',
      }
    }

    const result = await request('http://u.y.qq.com/cgi-bin/musicu.fcg?data=' + JSON.stringify({
        comm: {
          ct: 24,
          cv: 0
        },
        singerAlbum: {
          method: "get_singer_album",
          param: {
            singermid,
            order: "time",
            begin: (pageNo - 1) * pageSize,
            num: pageSize || 1,
            exstatus: 1
          },
          module: "music.web_singer_info_svr"
        }
    }));

    if (Number(raw)) {
      return result;
    } else {
      const { list, singer_id: id, singer_mid: singermid, singer_name: name, total } = result.singerAlbum.data;
      cacheData = {
        result: 100,
        data: {
          list,
          id,
          singermid,
          name,
          total,
          pageNo,
          pageSize,
        }
      };
      return cacheData;
    }
  },

  // 获取歌手分类
  category: async (query) => {
    const {raw} = query;

    const result = await request('https://u.y.qq.com/cgi-bin/musicu.fcg?data='+JSON.stringify({
          "comm": {"ct": 24, "cv": 0},
          "singerList": {
            "module": "Music.SingerListServer",
            "method": "get_singer_list",
            "param": {"area": -100, "sex": -100, "genre": -100, "index": -100, "sin": 0, "cur_page": 1}
          }
        })
    );

    if (Number(raw)) {
      return result;
    } else {
      return {
        result: 100,
        data: result.singerList.data.tags,
      }
    }
  },

  // 根据类型获取歌手列表
  list: async (query) => {
    const {area = -100, sex = -100, genre = -100, index = -100, pageNo = 1, raw} = query;

    const result = await request('https://u.y.qq.com/cgi-bin/musicu.fcg?data='+JSON.stringify({
            comm: {
              ct: 24,
              cv: 0
            },
            singerList: {
              module: "Music.SingerListServer",
              method: "get_singer_list",
              param: {
                area: area || 1,
                sex: sex || 1,
                genre: genre || 1,
                index: index || 1,
                sin: (pageNo - 1) * 80,
                cur_page: pageNo || 1,
              }
            }
          })
    );

    if (Number(raw)) {
      return result;
    } else {
      const trueData = result.singerList.data;
      trueData.list = trueData.singerlist;
      delete trueData.tags;
      delete trueData.singerlist;
      return {
        result: 100,
        data: trueData,
      }
    }
  },
  // 获取热门歌曲
  songs: async (query) => {
    const {singermid, num, raw, page = 1} = query;
    const pageSize = num ? parseInt(num) : 20;

    if (!singermid) {
      return {
        result: 500,
        errMsg: 'singermid 不能为空',
      }
    }
    let data = JSON.stringify({
        comm: {
          ct: 24,
          cv: 0
        },
        singer: {
          method: "get_singer_detail_info",
          param: {
            sort: 5,
            singermid,
            sin:  (page - 1) * num,
            num: pageSize,
          },
          module: "music.web_singer_info_svr"
        }
      });

    const result = await request('http://u.y.qq.com/cgi-bin/musicu.fcg?data='+data);

    if (Number(raw)) {
      return result;
    } else {
      const { songlist: list, singer_info: singer, singer_brief, desc, total_song: total, extras } = result.singer.data;
      list.forEach((o, i) => {
        Object.assign(o, extras[i] || {});
      });

      cacheData = {
        result: 100,
        data: {
          list,
          singer,
          desc,
          total,
          num: pageSize,
          singermid,
        }
      };
      return cacheData;

    }
  },
}
